package com.iemes.controller.mould;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.LoadDailyReportFormMap;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.UlReportFormMap;
import com.iemes.entity.ZInstallcheackFromMap;
import com.iemes.mapper.mould.LoadDailyReportMapper;
import com.iemes.mapper.mould.MouldUnpassHandleMapper;
import com.iemes.mapper.mould.UlReportMapper;
import com.iemes.mapper.mould.ZInstallcheackMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;


@RequestMapping("/installd/")
@Controller
public class InstalldailyreportController extends BaseController {

	@Inject
	private LoadDailyReportMapper loadDailyReportMapper;
	
	@Inject
	private UlReportMapper ulReportMapper;
	
	@Inject
	private MouldUnpassHandleMapper mouldUnpassHandleMapper;
	
	@Inject
	private ZInstallcheackMapper zInstallcheackMapper;
	
	//装模日报表
	@RequestMapping("/examineOrder")
	public String appointment(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/installdailyreport_list";
	}
	
	@ResponseBody
	@RequestMapping("Installd_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		LoadDailyReportFormMap loadDailyReportFormMap=getFormMap(LoadDailyReportFormMap.class);
		List<Map<String, Object>> loadDaily = loadDailyReportMapper.getAll(loadDailyReportFormMap);
		LuogicPaging(pageNow, pageSize, loadDaily);
		return pageView;
	}
	
	@RequestMapping("installDaily_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询装模日报表
			LoadDailyReportFormMap loadDaily=loadDailyReportMapper.getById(id);
			model.addAttribute("loadDaily",loadDaily);
		}
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/installdailyreport_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="installDaily_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="装模质量检查",methods="装模日报表-编辑装模日报表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		LoadDailyReportFormMap loadDailyReportFormMap=getFormMap(LoadDailyReportFormMap.class);
		baseMapper.editEntity(loadDailyReportFormMap);
		return "success";
	}
	
	//生产车间拆装模统计表
	@RequestMapping("/ulReport_list")
	public String listUl(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/ulReport_list";
	}
	
	@ResponseBody
	@RequestMapping("ulReport_findByPage")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="装模质量检查",methods="生产车间拆装模统计表-新增生产车间拆装模统计表")//凡需要处理业务逻辑的.都需要记录操作日志
	public PageView findByUlPage(String pageNow, String pageSize,HttpServletRequest request) throws Exception 
	{
		UlReportFormMap ulReportFormMap=getFormMap(UlReportFormMap.class);
		//以下代码会导致列表不能查询，须优化
		baseMapper.deleteByAttribute("id", ulReportFormMap.getStr("id"), UlReportFormMap.class);
		LoadDailyReportFormMap loadDailyReportFormMap=getFormMap(LoadDailyReportFormMap.class);
		//先查出所有装模日报表
		List<LoadDailyReportFormMap> loadDaily = loadDailyReportMapper.getAll2();
		for(LoadDailyReportFormMap l:loadDaily){
			String checkOnlyDate=l.getStr("checkOnlyDate");
			String iSchedule=l.getStr("iSchedule");
			if(checkOnlyDate!=null && iSchedule!=null){
				loadDailyReportFormMap.put("checkOnlyDate", checkOnlyDate);
				loadDailyReportFormMap.put("iSchedule", iSchedule);
				//再根据日期和班表查出所有装模日报表，并用distinct剔除重复数据
				List<LoadDailyReportFormMap> getAllByCI = loadDailyReportMapper.getAllByCI(loadDailyReportFormMap);
				int timeCost=0,waitCost=0,averageCost=0,failCount=0;
				StringBuffer sbf=new StringBuffer();
				for(int i=0;i<getAllByCI.size();i++){
					String iStaff1=getAllByCI.get(i).getStr("iStaff1");
					String iStaff2=getAllByCI.get(i).getStr("iStaff2");
					if(iStaff1!=null){
						sbf.append(iStaff1+"，");
					}
					if(iStaff2!=null){
						sbf.append(iStaff2+"，");
					}
					try {
						int finishTime=Integer.parseInt(getAllByCI.get(i).getStr("finishTime"));
						//计算装模总耗时
						timeCost+=finishTime;
						//计算averageCost
						averageCost+=finishTime/getAllByCI.size();
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					try {
						int installWaiting=Integer.parseInt(getAllByCI.get(i).getStr("installWaiting"));
						//计算待装模时间
						waitCost+=installWaiting;
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					try {
						int judge=getAllByCI.get(i).getInt("judge");
						if(judge==2){
							//计算不合格装模数量
							failCount++;
						}
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
				ulReportFormMap=new UlReportFormMap();
				ulReportFormMap.put("createDate", checkOnlyDate);
				ulReportFormMap.put("schedule", iSchedule);
				try {
					String[] str=sbf.substring(0,sbf.length()-1).split("，");
					ArrayList<String> list = new ArrayList<String>();
					for (int i = 0; i < str.length; i++) {
						if (!list.contains(str[i])) {
							list.add(str[i]);
						}
					}
					ulReportFormMap.put("staffs", list);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				ulReportFormMap.put("actualLoad", getAllByCI.size());
				ulReportFormMap.put("timeCost", timeCost);
				ulReportFormMap.put("waitCost", waitCost);
				ulReportFormMap.put("averageCost", averageCost);
				ulReportFormMap.put("overTime", "？？？");
				ulReportFormMap.put("failCount", failCount);
				ulReportFormMap.put("passRate", (getAllByCI.size()-failCount)/getAllByCI.size()*100);
				baseMapper.addEntity(ulReportFormMap);
			}
		}
		List<Map<String, Object>> ulReport = ulReportMapper.getAll(ulReportFormMap);
		LuogicPaging(pageNow, pageSize, ulReport);
		return pageView;
	}
	
	//待装模时间统计表
	@RequestMapping("/waitReport_list")
	public String listWUI(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/waitReport_list";
	}
	
	@ResponseBody
	@RequestMapping("waitReport_findByPage")
	public PageView waitFindByPage(String pageNow, String pageSize) throws Exception 
	{
		LoadDailyReportFormMap loadDailyReportFormMap=getFormMap(LoadDailyReportFormMap.class);
		List<Map<String, Object>> loadDaily = loadDailyReportMapper.getAll(loadDailyReportFormMap);
		LuogicPaging(pageNow, pageSize, loadDaily);
		return pageView;
	}
	
	@RequestMapping("waitReport_edit")
	public String editWUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询待装模时间统计表
			LoadDailyReportFormMap loadDaily=loadDailyReportMapper.getById(id);
			model.addAttribute("loadDaily",loadDaily);
		}
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/waitReport_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="waitReport_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="装模质量检查",methods="待装模时间统计表-修改待装模时间统计表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editWEntity() throws Exception {
		LoadDailyReportFormMap loadDailyReportFormMap=getFormMap(LoadDailyReportFormMap.class);
		try {
			baseMapper.editEntity(loadDailyReportFormMap);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "success";
	}
	
	//装模质量不合格统计表
	@RequestMapping("/unpassReport_list")
	public String listUnpassUI(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/unpassReport_list";
	}
	
	@ResponseBody
	@RequestMapping("unpassReport_findByPage")
	public PageView unpassFindByPage(String pageNow, String pageSize) throws Exception 
	{
		MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
		mouldUnpassHandleFormMap.put("checkResult", 2);
		List<Map<String, Object>> report = mouldUnpassHandleMapper.getInstallDaily(mouldUnpassHandleFormMap);
		LuogicPaging(pageNow, pageSize, report);
		return pageView;
	}
	
	//超时装模统计表
	@RequestMapping("/overtimeReport_list")
	public String listOTUI(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/overtimeReport_list";
	}
	
	@ResponseBody
	@RequestMapping("overtimeReport_findByPage")
	public PageView overtimeFindByPage(String pageNow, String pageSize) throws Exception 
	{
		LoadDailyReportFormMap loadDailyReportFormMap=getFormMap(LoadDailyReportFormMap.class);
		//“总耗时”超45分钟在哪里拿数据？？？
		
		List<Map<String, Object>> loadDaily = loadDailyReportMapper.getAll(loadDailyReportFormMap);
		LuogicPaging(pageNow, pageSize, loadDaily);
		return pageView;
	}
	
	@RequestMapping("overtimeReport_edit")
	public String editOTUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询待装模时间统计表
			LoadDailyReportFormMap loadDaily=loadDailyReportMapper.getById(id);
			model.addAttribute("loadDaily",loadDaily);
		}
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/overtimeReport_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="overtimeReport_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="装模质量检查",methods="待装模时间统计表-修改待装模时间统计表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editOTEntity() throws Exception {
		LoadDailyReportFormMap loadDailyReportFormMap=getFormMap(LoadDailyReportFormMap.class);
		try {
			baseMapper.editEntity(loadDailyReportFormMap);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "success";
	}
	
	//重复装模统计表
	@RequestMapping("/reloadReport_list")
	public String listRUI(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/installdailyreport/reloadReport_list";
	}
	
	@ResponseBody
	@RequestMapping("reloadReport_findByPage")
	public PageView ReloadfindByPage(String pageNow, String pageSize) throws Exception 
	{
		ZInstallcheackFromMap zInstallcheackFromMap=getFormMap(ZInstallcheackFromMap.class);
		List<Map<String, Object>> reloadReport = zInstallcheackMapper.getByLT(zInstallcheackFromMap);
		LuogicPaging(pageNow, pageSize, reloadReport);
		return pageView;
	}
}









